/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.delivery.web;

import java.util.Date;
import java.util.List;

import com.javaeye.delivery.dto.Reason;
import com.javaeye.delivery.service.ReasonService;
import com.javaeye.common.util.DateUtils;
import com.javaeye.common.util.NumberUtils;
import com.javaeye.common.web.BaseReportAction;
import com.javaeye.common.web.ReportData;

public class ReasonReport extends BaseReportAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1998966732318406119L;
	
	private ReasonService service;
	
	private List<Reason> reasonList;
	
	private int queryDeptId;
	
	public ReasonReport() {
		setQueryDateType(1);//当月
		queryDeptId = -1;
	}
	
	public void setService(ReasonService service) {
		this.service = service;
	}
	
	public String queryReasonList() throws Exception {
		Date beginDate = DateUtils.getCurrentMonthBeginDate();
		Date endDate = DateUtils.getCurrentMonthEndDate();
		int queryDateType = getQueryDateType();
		switch(queryDateType) {
		case 1 :
			break;
		case 2 :
			beginDate = DateUtils.getBeforeMonthBeginDate();
			endDate = DateUtils.getBeforeMonthEndDate();
			break;
		default :
			break;
		}
		reasonList = service.getReasonList(beginDate, endDate, queryDeptId);
		return SUCCESS;
	}
	
	public String queryReasonData() throws Exception {
		Date beginDate = DateUtils.getCurrentMonthBeginDate();
		Date endDate = DateUtils.getCurrentMonthEndDate();
		int queryDateType = getQueryDateType();
		switch(queryDateType) {
		case 1 :
			break;
		case 2 :
			beginDate = DateUtils.getBeforeMonthBeginDate();
			endDate = DateUtils.getBeforeMonthEndDate();
			break;
		default :
			break;
		}
		List<ReportData> result = service.getReasonData(beginDate, endDate);
		int totleCount = getTotleCount(result);
		double precent;
		for (ReportData data : result) {
			precent = (double)data.getData() / totleCount * 100;
			data.setPrecent(NumberUtils.formatDouble(precent));
		}
		setReportDatas(result);
		return SUCCESS;
	}
	
	private int getTotleCount(List<ReportData> datas) {
		int retVal = 0;
		for (ReportData data : datas) {
			retVal += data.getData();
		}
		return retVal;
	}

	public List<Reason> getReasonList() {
		return reasonList;
	}

	public void setReasonList(List<Reason> reasonList) {
		this.reasonList = reasonList;
	}

	public int getQueryDeptId() {
		return queryDeptId;
	}

	public void setQueryDeptId(int queryDeptId) {
		this.queryDeptId = queryDeptId;
	}
	
	
}
